package com.firebird.app.kotlin.stock.ui.allocation_plan.presenter

import com.firebird.app.kotlin.stock.App
import com.firebird.app.kotlin.stock.base.BasePresenter
import com.firebird.app.kotlin.stock.data.MoneyRuleBean
import com.firebird.app.kotlin.stock.data.MultipleRuleBean
import com.firebird.app.kotlin.stock.ui.allocation_plan.contract.AllocationPlanContract
import com.firebird.app.kotlin.stock.ui.allocation_plan.model.AllocationPlanModel
import com.firebird.app.kotlin.stock.utils.SharedPreferencesUtil
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response


class AllocationPlanPresenter : BasePresenter<AllocationPlanContract.View?>(), AllocationPlanContract.Presenter {
    private val model: AllocationPlanContract.Model
    override fun getMoneyRuleData(bpType: Int) {
        if (!isViewAttached) {
            return
        }
        model.getMoneyRuleData(bpType)!!.enqueue(object : Callback<MoneyRuleBean?> {
            override fun onResponse(call: Call<MoneyRuleBean?>, response: Response<MoneyRuleBean?>) {
                mView!!.onSuccess(response.body())
                SharedPreferencesUtil.getInstance(App.context!!)!!.putSP("token", response.headers()["token"])
            }

            override fun onFailure(call: Call<MoneyRuleBean?>, t: Throwable) {
                mView!!.onError(t)
            }
        })
    }

    override fun getMultipleRuleData(bpType: Int) {
        if (!isViewAttached) {
            return
        }
        mView!!.showLoading()
        model.getMultipleRuleData(bpType)!!.enqueue(object : Callback<MultipleRuleBean?> {
            override fun onResponse(call: Call<MultipleRuleBean?>, response: Response<MultipleRuleBean?>) {
                mView!!.hideLoading()
                mView!!.onSuccess(response.body())
                SharedPreferencesUtil.getInstance(App.context!!)!!.putSP("token", response.headers()["token"])
            }

            override fun onFailure(call: Call<MultipleRuleBean?>, t: Throwable) {
                mView!!.hideLoading()
                mView!!.onError(t)
            }
        })
    }

    init {
        model = AllocationPlanModel()
    }
}